package com.zcmob.web.admanagement.vo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

public class BatchUtils {
    public static void packAndSign(String rootPath, String packagename) {
        OutputStream os = null;
        InputStream is = null;
        try {
            // 打开cmd进程
            Process process = Runtime.getRuntime().exec("cmd");

            os = process.getOutputStream();
            is = process.getInputStream();

            // 查看cmd版本命令
            runCommand(os, "d:\n");
            System.out.println("d:");

            runCommand(os, "cd " + rootPath + "\n");
            System.out.println("cd");
            // cd命令
            runCommand(os, "apktool b " + packagename + " " + packagename + "_unsigned.apk\n");
            System.out.println("apk b");

            String signCmd = "jarsigner -verbose -keystore  my-release-key.keystore -storepass walterpei -keypass walterpei -signedjar output/" + packagename + "_singed.apk " + packagename + "_unsigned.apk mykey";
            runCommand(os, signCmd + "\n");
            System.out.println("signed");

            runCommand(os, "exit\n");
            System.out.println("exit");

            System.out.println("destroy");
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            while (true) {
                String s = br.readLine();
                System.out.println(s);
                if (s == null) {
                    break;
                }

            }
            process.destroy();
            System.out.println("finish");

            br.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void runCommand(OutputStream os, String command) throws IOException {
        os.write(command.getBytes());
        os.flush();
    }
}
